<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
/***************************
傳進參數
GambleID：操盤序號
LottoID：樂透種類序號
isSpecial：是否為特碼
WagerContent：下注號碼#賠率序號#下注金額#下注種類@下注號碼#賠率序號#下注金額#下注種類@.........

ps. 此下注方法目前僅適合特碼、特單雙、特大小、正碼、總和單雙、總和大小，其他一律不適用
***************************/
include_once("../config.inc.php");

$_LangPath = "wagerinsert";	//語言檔參數，務必放在最前面
include_once(__Member_Path."/logincheck.php");
include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/LogRecord.lib.php");
$objMemberInfo = $_SESSION["Member_Info"];

if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$isSpecial = (int)$_data["isSpecial"];
$_data["CheckKey1"] = trim($_data["CheckKey1"]);
$_data["CheckKey2"] = trim($_data["CheckKey2"]);
$_data["WagerContent"] = trim($_data["WagerContent"]);

$intGap = $isSpecial - 1; //若是正碼，下注種類序號追加 3
if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}

//檢查關鍵參數是否為空值
if(!$intLID || !$intLType || strlen($_data["CheckKey2"]) == 0 || strlen($_data["CheckKey1"]) == 0 || empty($intGID) || strlen($_data["WagerContent"]) == 0){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["BetAgain"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	//print $intLID."=".$_data["CheckKey2"]."=".$_data["CheckKey1"]."=".$intGID."=".$_data["WagerContent"];
	include("wager_error.php");
}

include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");
include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
$arrWC = split("@", $_data["WagerContent"]);
if(sizeof($arrWC) == 0){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}

//賠率轉換值
$isAcceptLimit = false;
if($intLID == 4){
	include_once(__Common_Path."/CommToOddsRate_".$intLID.".inc.php");
	include_once(__Modules_Path."/Commission.lib.php");
	//找出 使用者 的賠率兌換，傳回陣列
	$arrUserOPlus = getUserCommToOdds($objMemberInfo -> ID, $intLID, array());
	if($arrUserOPlus === false)ErrMsgOutputHTML("Error", "", 1);
	$arrUserOPlus = $objMemberInfo -> UserCommToOdds[$intLID];
	$isAcceptLimit = true;
}
//判斷是否啟動自動向上補貨
$isAutoSupple = false;
if(($objMemberInfo -> SubIsAutoSupple || $objMemberInfo -> MainIsAutoSupple || $objMemberInfo -> StockIsAutoSupple || $objMemberInfo -> DirectorIsAutoSupple)){//$intLID == 1 && 
	$isAutoSupple = true;
}

/***************************************************************************************************************/
$arrTmpGapBT[1] = array(1,2,3,4,5,6,16,21,22,23,24,25,26,28);//
$arrTmpBT[1] = array(1,2,3,4,5,6,12,13,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29);
$arrTmpBT[2] = array(51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,84,85,86,92,93);
$arrTmpBT[3] = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,18,19,20,21,22);
$arrTmpBT[4] = array(1,2,3,4,5,6,7,8);
$arrPermitBT = $arrTmpBT[$intLType]; //允許下注種類
$arrGapBT = $arrTmpGapBT[$intLType]; //允許下注種類
$arrBT = array();
$arrNum = array();
while(list($k, $strWData) = each($arrWC)){
	if($strWData == "")continue;

	$arrBData = explode("#", $strWData);
	//檢查關鍵參數是否為空值
	if(strlen($arrBData[0]) == 0 || strlen($arrBData[1]) == 0 || strlen($arrBData[2]) == 0 || strlen($arrBData[3]) == 0){
		$Err_Title = $_strWagerInsert["BetError"];
		$Err_Message = $_strWagerInsert["BetAgain"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
	//檢查下注金額是否為數值
	if(!preg_match("/^[0-9]{0,7}$/", $arrBData[2])){
		$Err_Title = $_strWagerInsert["BetError"];
		$Err_Message = $_strWagerInsert["StakeisString"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
	//不正確下注模式
	$intBT = (int)$arrBData[3];
	if(is_array($arrGapBT)){
		if(in_array($intBT, $arrGapBT)){ //若為特碼B
			if(!($isSpecial == 28 && $intBT != 1)){
				$intBT += $intGap;
			}
		}
	}

	if(!in_array($intBT, $arrPermitBT)){
		$Err_Title = $_strWagerInsert["BetError"].$intBT;
		$Err_Message = $_strWagerInsert["ErrBetType"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
	if(!is_array($_arrBetParam[$intBT])){
		$Err_Message = "No Wager Relation Data!!";
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}

	//退水轉賠率
	//$arrComOddsPlus[$intBT] = $arrUserOPlus[$intBT][11] * $_arrChRate[$intBT][$objMemberInfo -> GambleType+10];
	//$flCommPlus = $arrUserOPlus[$intBT][11] * 1;
	$flCommPlus = 0;
	
	//會員、代理商、總代理、股東退水質不得為空值
	$arrComm[$intBT][1] = $objMemberInfo -> arrCommission[$intLID][$intBT] + $flCommPlus;	 //會員退水值
	$arrComm[$intBT][2] = $objMemberInfo -> arrSubCommission[$intLID][$intBT] + $flCommPlus;			//代理商退水值
	$arrComm[$intBT][3] = $objMemberInfo -> arrMainCommission[$intLID][$intBT] + $flCommPlus;			//總代理退水值
	$arrComm[$intBT][4] = $objMemberInfo -> arrStockCommission[$intLID][$intBT] + $flCommPlus;		//股東退水值
	$arrComm[$intBT][5] = $objMemberInfo -> arrDirectorCommission[$intLID][$intBT] + $flCommPlus;	//總監退水值
	$arrComm[$intBT][6] = $objMemberInfo -> arrLayer6Commission[$intLID][$intBT] + $flCommPlus;	//業務總監退水值
	if(empty($arrComm[$intBT][1]) || empty($arrComm[$intBT][2]) || empty($arrComm[$intBT][3]) || empty($arrComm[$intBT][4]) || empty($arrComm[$intBT][5]) || empty($arrComm[$intBT][6]) || 
		$arrComm[$intBT][5] > $arrComm[$intBT][4] || $arrComm[$intBT][4] > $arrComm[$intBT][3] || $arrComm[$intBT][3] > $arrComm[$intBT][2] || $arrComm[$intBT][2] > $arrComm[$intBT][1] || $arrComm[$intBT][5] < 70 || $arrComm[$intBT][5] > 100		
	){
		$Err_Title = $_strWagerInsert["BetError"];
		$Err_Message = $_strWagerInsert["CommissionErr"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		//print "\n\n".$intBT."\n\n";
		//print_r($arrComm[$intBT]);
		include("wager_error.php");
	}

	$arrBData[0] = intval($arrBData[0]);		//號碼
	$arrBData[1] = intval($arrBData[1]);		//賠率序號
	$arrBData[2] = intval($arrBData[2]);		//注金
	if($arrBData[0] < 0 || !($arrBData[1] > 0) || !($arrBData[2] > 0))continue;
	$arrBData[3] = $intBT;
	$arrWC2[] = $arrBData;
	$sumGold += $arrBData[2];		//累積此次下注總金額
	$arrBallGold[$intBT][$arrBData[0]] += $arrBData[2];		//累積此次各球類及下注方法總注金
	$arrBT[] = $intBT;
	$arrNum[] = $arrBData[0];
}
//分盤賠率
include_once(__Common_Path."/gambletype/GambleType1_".$intLID.".inc.php");
$arrGTAllOdds = $_arrGambleTypeAllOdds;
$arrGambleTypeAS = $_arrGambleType;

include_once(__Common_Path."/gambletype/GambleType".$objMemberInfo -> GambleType."_".$intLID.".inc.php");

PMA_mysql_query("BEGIN", $_ConnMaster);
$_arrMemStake = getWagerArrayGoldByDB($objMemberInfo -> ID);
if($_arrMemStake === false){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

/***************************************************************************************************************/
//下注金額不能大於信額額度
if($sumGold > ($objMemberInfo -> CreditLine - $_arrMemStake["_SUM"])){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["NotOverCredit"];
	$Err_Message .= $_strWagerInsert["MemCredit"]."(".number_format($objMemberInfo -> CreditLine).")<br>";
	$Err_Message .= $_strWagerInsert["UsedCredit"]."(".number_format($_arrMemStake["_SUM"]).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

//下注金額不能大於上層信額額度
if(isOverParentCredit($sumGold*$flMemERate, $objMemberInfo -> SubID, $objMemberInfo -> SubCreditLine, $objMemberInfo -> MainID, $objMemberInfo -> MainCreditLine)){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	if($GLOBALS["errormessage"] == "")$Err_Message = $_strWagerInsert["NotOverCredit"];
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
/***************************************************************************************************************/
//include_once(__Modules_Path."/RealBet.lib.php");
//$strParentID = $objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID;
if($isAcceptLimit || $isAutoSupple){
	$arrLayerID = array(2 => $objMemberInfo -> SubID, 3 => $objMemberInfo -> MainID, 4 => $objMemberInfo -> StockID, 5 => $objMemberInfo -> DirectorID, 6 => $objMemberInfo -> Layer6ID);

	$arrRealStake = getUserUpLayerRealStake($intLID, $intGID, $arrBT, $arrNum, $arrLayerID);
	if($arrRealStake === false){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
		$Err_HiddenMessage = $GLOBALS["errorhidden"];
		include("wager_error.php");
	}
}

$arrAutoSuppleGold = array();
$arrAutoSuppleGroup = array();
while(list($intBT, $v) = each($arrBallGold)){
	$isAutoSuppleBT = $isAutoSupple;

	$BetLimit = $objMemberInfo -> arrBetLimit[$intLID][$intBT];
	$EventLimit = $objMemberInfo -> arrEventLimit[$intLID][$intBT];
	$arrAcceptLimit = $objMemberInfo -> arrAcceptLimit[$intLID][$intBT];
	$arrAutoSuppleLimit = ($intLID == 1 && $intBT == 28)?$objMemberInfo -> arrAutoSuppleLimit[$intLID][1]:$objMemberInfo -> arrAutoSuppleLimit[$intLID][$intBT];

	if($_arrLottoLimit[$intBT][0] > 0){
		$_arrLottoLimit[$intBT][0] = floor($_arrLottoLimit[$intBT][0] / $flMemERate);
		if($_arrLottoLimit[$intBT][0] < $EventLimit)$EventLimit = $_arrLottoLimit[$intBT][0];
	}

	if($_arrLottoLimit[$intBT][1] > 0){
		$_arrLottoLimit[$intBT][1] = floor($_arrLottoLimit[$intBT][1] / $flMemERate);
		if($_arrLottoLimit[$intBT][1] < $BetLimit)$BetLimit = $_arrLottoLimit[$intBT][1];
	}

	while(list($intNum, $intGold) = each($v)){
		//找出 Apoint
		$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$intNum];

		if($intLID == 1 && $intBT == 28){
			if(!$_arrBetParam[1][$intAN]["isAutoSupple"])$isAutoSuppleBT = false;
		}else{
			if(!$_arrBetParam[$intBT][$intAN]["isAutoSupple"])$isAutoSuppleBT = false;
		}
		//公司單注限額
		$Com_BetLimit = floor($_arrBetParam[$intBT][$intAN]["Com_BetLimit"] / ($objMemberInfo -> Layer6Scale*$flMemERate));
		if($Com_BetLimit < $BetLimit)$BetLimit = $Com_BetLimit;

		$intMinStake = floor($_arrBetParam[$intBT][$intAN]["MinStake"] / $flMemERate);
		//下注金額不能小於最小下注金額
		if($intGold < $intMinStake){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Title = $_strWagerInsert["BetError"];
			$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
			$Err_Message = $_strWagerInsert["NotUnderMinStake"]."(".$intGold.",".number_format($intMinStake).")<br />".$strBTTip;
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
		//下注金額不能大於單注限額
		if($intGold > $BetLimit){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Title = $_strWagerInsert["BetError"];
			$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
			$Err_Message = $_strWagerInsert["NotOverMaxStake"]."(".number_format($BetLimit).")<br />".$strBTTip;
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
		//下注金額不能大於單場限額
		$intEvent = $_arrMemStake[$intBT][$intGID][$intNum];
		if($intGold > ($EventLimit - $intEvent)){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Title = $_strWagerInsert["BetError"];
			$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
			$Err_Message = $_strWagerInsert["NotOverEvenLimit"];
			$Err_Message .= $_strWagerInsert["EvenLimit"]."(".number_format($EventLimit).")<br />";
			$Err_Message .= $_strWagerInsert["ThisStake"]."(".number_format($intEvent).")<br />".$strBTTip;
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
		/*if(!$strTop = GetOneStopWagerBet($intGID, $intBT, $intNum, $strParentID)){
			$Err_Message = $GLOBALS["errormessage"];
			$Err_HiddenMessage = $GLOBALS["errorhidden"];
			include("wager_error.php");
		}
		if($strTop == "Stop"){
			$Err_Message = $_strWagerInsert["StopWager"];
			include("wager_error.php");
		}*/
		//下注額度不能大於上層可接收注額
		if(!$isAutoSuppleBT && !$isAcceptLimit)continue;
		if(!$isAutoSuppleBT && $isAcceptLimit && ($intBT < 7 || $intBT > 11))continue;

		foreach($arrLayerID as $intGroup => $intUID){
			if($intUID == $objMemberInfo -> ID)continue;
			if(!$isAutoSuppleBT && $isAcceptLimit && (float)$arrAcceptLimit[$intUID] <= 0)continue;
			//if($arrAutoSuppleLimit[$intUID] < 0)$arrAutoSuppleLimit[$intUID] = 0;

			$flRealStake = 0;
			$isLayerAutoSupple = false;
			$arrAutoSuppleGroupGold[$intBT][$intNum][$intGroup] = 0; //各階層自動補貨金額
			switch($intGroup){
				case 2:
					$flRealStake = $flMemERate*$intGold*(1 - $objMemberInfo -> SubScale);
					$isLayerAutoSupple = $objMemberInfo -> SubIsAutoSupple;
					break;

				case 3:
					$flRealStake = $flMemERate*$intGold*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
					$flRealStake += $arrAutoSuppleGroupGold[$intBT][$intNum][2]*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);

					$isLayerAutoSupple = $objMemberInfo -> MainIsAutoSupple;
					break;

				case 4:
					$flRealStake = $flMemERate*$intGold*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
					$flRealStake += ($arrAutoSuppleGroupGold[$intBT][$intNum][2] + $arrAutoSuppleGroupGold[$intBT][$intNum][3])*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);

					$isLayerAutoSupple = $objMemberInfo -> StockIsAutoSupple;
					break;

				case 5:
					$flRealStake = $flMemERate*$intGold*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
					//$flRealStake += ($arrAutoSuppleGroupGold[$intBT][$intNum][2] + $arrAutoSuppleGroupGold[$intBT][$intNum][3] + $arrAutoSuppleGroupGold[$intBT][$intNum][4])*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
					$flRealStake += $arrAutoSuppleGroupGold[$intBT][$intNum][2]*($objMemberInfo -> LayerScale[2][4] - $objMemberInfo -> LayerScale[2][5]) + $arrAutoSuppleGroupGold[$intBT][$intNum][3]*($objMemberInfo -> LayerScale[3][4] - $objMemberInfo -> LayerScale[3][5]) + $arrAutoSuppleGroupGold[$intBT][$intNum][4]*($objMemberInfo -> LayerScale[4][4] - $objMemberInfo -> LayerScale[4][5]);

					$isLayerAutoSupple = $objMemberInfo -> DirectorIsAutoSupple;
					break;

				case 6:
					$flRealStake = $flMemERate*$intGold*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
					$flRealStake += $arrAutoSuppleGroupGold[$intBT][$intNum][2]*($objMemberInfo -> LayerScale[2][5] - $objMemberInfo -> LayerScale[2][6]) + $arrAutoSuppleGroupGold[$intBT][$intNum][3]*($objMemberInfo -> LayerScale[3][5] - $objMemberInfo -> LayerScale[3][6]) + $arrAutoSuppleGroupGold[$intBT][$intNum][4]*($objMemberInfo -> LayerScale[4][5] - $objMemberInfo -> LayerScale[4][6]) + $arrAutoSuppleGroupGold[$intBT][$intNum][5]*($objMemberInfo -> LayerScale[5][5] - $objMemberInfo -> LayerScale[5][6]);

					$isLayerAutoSupple = $objMemberInfo -> Layer6IsAutoSupple;
					break;
			}

			if($isAcceptLimit && $arrAcceptLimit[$intUID] > 0 && $arrRealStake[$intBT][$intNum][$intUID] + $flRealStake > $arrAcceptLimit[$intUID]){
				PMA_mysql_query("ROLLBACK", $_ConnMaster);
				//if($GLOBALS["errormessage"] == "")
				$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
				$Err_Message = $_strWagerInsert["NotOverAccept"]."<br />".$strBTTip;
				$Err_Title = $_strWagerInsert["BetError"];
				$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
				include("wager_error.php");
			}
			//print $isLayerAutoSupple."QQQ";
			if($isLayerAutoSupple){
				//print "Auto<br />";
				//print $intBT." = ".$intNum." => ".$_arrBetParam[$intBT]["AutoSuppleOverGold"]." + ".$arrAutoSuppleLimit[$intUID]." <.". $arrRealStake[$intBT][$intNum][$intUID]." + ".$flRealStake."<br />\n";
				$intBTTmp = $intBT;
				if($intLID == 1 && $intBTTmp == 28)$intBTTmp = 1;

				if($arrAutoSuppleLimit[$intUID] < 0 || $arrAutoSuppleLimit[$intUID] == null)continue;//$arrAutoSuppleLimit[$intUID] = 0;
				if($_arrBetParam[$intBTTmp][$intAN]["isAutoSupple"] == 1 && ($_arrBetParam[$intBTTmp][$intAN]["AutoSuppleOverGold"] + $arrAutoSuppleLimit[$intUID]) <= ($arrRealStake[$intBTTmp][$intNum][$intUID] + $flRealStake)){
					$arrAutoSuppleGold[$intBT][$intNum][$intUID] = $arrRealStake[$intBTTmp][$intNum][$intUID] + $flRealStake - $arrAutoSuppleLimit[$intUID];
					print $arrRealStake[$intBTTmp][$intNum][$intUID]." + ".$flRealStake." - ".$arrAutoSuppleLimit[$intUID]."\n";
					$arrAutoSuppleGroup[$intBT][$intNum][$intUID] = $intGroup;
					$arrAutoSuppleGroupGold[$intBT][$intNum][$intGroup] = $arrAutoSuppleGold[$intBT][$intNum][$intUID];
				}
			}
		} // foreach($arrLayerID as $intGroup => $intUID){
	}
}
/*print_r($arrAutoSuppleGold);
print_r($arrAutoSuppleGroup);
print_r($arrAutoSuppleGroupGold);
exit();*/
/***************************************************************************************************************/

$j = 0;
$arrLogData = array();
$arrTmpOddsPlus[1] = array(2,3,5,6,12,13,16,27);
$arrTmpOddsPlus[2] = array(52,53,55,56,58,59,62,63,84,85,86);
$arrTmpOddsPlus[3] = array(14,15,16,17);
$arrTmpOddsPlus[4] = array(2,3,4);
$arrOddsPlus = $arrTmpOddsPlus[$intLType]; //需賠率 +1 的 下注種類

$arrCommission = array();
if($isAutoSupple && is_array($arrAutoSuppleGroup)){
	if(sizeof($arrAutoSuppleGroup) > 0){
		//找出代理商、總代理、股東、總監 A盤退水
		$strSQL = "select MemID, Bet_Type, Commission from `bet_Commission` USE INDEX (MemID) where MemID in (".$objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID.",".$objMemberInfo -> DirectorID.") and Gamble_Type = 1 and LottoID = ".$intLID." and Bet_Type in (".implode(",", $arrBT).")";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$Err_Message = "Find Commission Data Error!!";
			//print $strSQL;
			include("wager_error.php");
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrCommission[$Rows -> MemID][$Rows -> Bet_Type] = 1 - ($Rows -> Commission / 100);
		}
	}
}

while(list($k, $arrBData) = each($arrWC2)){
	$intBT = $arrBData[3];			//下注種類序號
	$intNum = $arrBData[0];			//下注號碼球
	$intOddsID = $arrBData[1];	//賠率序號
	$intGold = $arrBData[2];			//下注金額
	if($_KSysParam[isOpenSpecialNumberBLine] != 1 && $intBT == 28)continue;
	$intAN = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
	if($intAN == null)$intAN = 0;
	//退水轉賠率
	$arrComOddsPlus[$intBT][$intAN] = $arrUserOPlus[$intBT][$intAN][11] * $_arrChRate[$intBT][$intAN][$objMemberInfo -> GambleType+10];
	$flCommPlus = $arrUserOPlus[$intBT][$intAN][11] * 1;

	//取得最新號碼賠率資料
	$arrGDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, $intNum, $intOddsID, 0);
	if($arrGDate["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_strWagerInsert[$arrGDate["Err_Message"]].$arrGDate["Err_Message"]."<br />".$strBTTip;
		include("wager_error.php");
	}elseif($arrGDate["isMessage"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_strWagerInsert[$arrGDate["Message"]]."<br />".$strBTTip;
		include("wager_error.php");
	}
	//特碼 46用
	$arrGDate["Bet_Type"] = $intBT;
	$flOdds = floatval($arrGDate["Odds"]) + floatval($arrComOddsPlus[$intBT][$intAN]);
	$flOddsAS = floatval($arrGDate["Odds"]); //自動向上補貨用賠率
	//加進分盤資料
	$flOdds += $_arrGambleType[$intBT][$intAN];
	$flOddsAS += $arrGambleTypeAS[$intBT][$intAN];
	if(in_array($intBT, $arrOddsPlus)){
		$flOdds += 1;
		$flOddsAS += 1;
	}

	$strWagerConditions = getWagerConditions($intLType, $arrGDate["Bet_Type"], $arrGDate["Number"], $flOdds, $arrGDate["SerialNo"]);
	$strWagerConditionsAS = getWagerConditions($intLType, $arrGDate["Bet_Type"], $arrGDate["Number"], $flOddsAS, $arrGDate["SerialNo"]);

	//組合陣列 for bet_BetListContent 用
	$arrWCont["GameDate_x"] = $arrGDate["GameDate"];
	$arrWCont["Content_x"] = $strWagerConditions;
	$strCheckKey = str_replace("0.", ($_data["CheckKey2"] + $j)."-", $_data["CheckKey1"]);
	$arrWCont["checkkey_x"] = $strCheckKey;
	$arrWCont["GambleType"] = $objMemberInfo -> GambleType;
	$arrWCont["MemAccount_x"] = $objMemberInfo -> Account;
	$arrWCont["isBlack"] = $objMemberInfo -> isBlack;

	$flMemPayOff = ($intGold*($flOdds - 1));
	//組合陣列 for bet_BetList 用
	$arrWList["LottoID"] = $arrGDate["LottoID"];
	$arrWList["LType"] = $intLType;
	$arrWList["Bet_Type"] = $arrGDate["Bet_Type"];
	$arrWList["GambleID"] = $arrGDate["GambleID"];
	$arrWList["Number"] = $arrGDate["Number"];
	$arrWList["OddsID"] = $arrGDate["OddsID"];
	$arrWList["Odds"] = $flOdds;
	$arrWList["MemID"] = $objMemberInfo -> ID;
	$arrWList["MemExchangeRate"] = $flMemERate;
	$arrWList["MemCurrency"] = $objMemberInfo -> ERID;
	$arrWList["ExRecordID"] = $objMemberInfo -> ExRecordID;
	$arrWList["MemStake"] = $intGold;
	$arrWList["MemPayOff"] = $flMemPayOff;
	$arrWList["MemCommission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][1] + $flCommPlus) / 100);
	$arrWList["SubID"] = $objMemberInfo -> SubID;
	$arrWList["SubCommission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][2] + $flCommPlus) / 100);
	$arrWList["SubRate"] = $objMemberInfo -> SubScale;
	$arrWList["MainID"] = $objMemberInfo -> MainID;
	$arrWList["MainCommission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][3] + $flCommPlus) / 100);
	$arrWList["MainRate"] = $objMemberInfo -> MainScale;
	$arrWList["StockID"] = $objMemberInfo -> StockID;
	$arrWList["StockCommission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][4] + $flCommPlus) / 100);
	$arrWList["StockRate"] = $objMemberInfo -> StockScale;
	$arrWList["DirectorID"] = $objMemberInfo -> DirectorID;
	$arrWList["DirectorCommission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][5] + $flCommPlus) / 100);
	$arrWList["DirectorRate"] = $objMemberInfo -> DirectorScale;
	$arrWList["Layer6ID"] = $objMemberInfo -> Layer6ID;
	$arrWList["Layer6Commission"] = 1 - (($arrComm[$arrGDate["Bet_Type"]][6] + $flCommPlus) / 100);
	$arrWList["Layer6Rate"] = $objMemberInfo -> Layer6Scale;
	$arrWList["PayupDate_x"] = $arrGDate["PayupDate"];

	//寫入注單
	$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
	if($arrBetData["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$Err_Message = $arrBetData["ErrorMessage"];
		$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"];
		include("wager_error.php");
	}
	
	$arrRealToday = array();
	$arrRealToday[$objMemberInfo -> SubID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
									"C" => $arrWList["MemStake"]*$arrWList["MemCommission"]*$flMemERate*(1 - $objMemberInfo -> SubScale), 
									"RS" => $arrWList["MemStake"]*$flMemERate*(1 - $objMemberInfo -> SubScale), 
									"RP" => $arrWList["MemPayOff"]*$flMemERate*(1 - $objMemberInfo -> SubScale));

	$arrRealToday[$objMemberInfo -> MainID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
									"C" => $arrWList["MemStake"]*$arrWList["SubCommission"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale), 
									"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale), 
									"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale));

	$arrRealToday[$objMemberInfo -> StockID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
									"C" => $arrWList["MemStake"]*$arrWList["MainCommission"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale), 
									"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale), 
									"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale));

	$arrRealToday[$objMemberInfo -> DirectorID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
									"C" => $arrWList["MemStake"]*$arrWList["StockCommission"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale), 
									"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale), 
									"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale));

	$arrRealToday[$objMemberInfo -> Layer6ID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
									"C" => $arrWList["MemStake"]*$arrWList["StockCommission"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale), 
									"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale), 
									"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale));

	$arrPlusStake = array("Stake" => 0, "RealStake" => 0, "PayOff" => 0, "RealPayOff" => 0);
	//自動補貨
	if($isAutoSupple && is_array($arrAutoSuppleGroup[$intBT][$intNum])){	
		foreach($arrAutoSuppleGroup[$intBT][$intNum] as $intUID => $intGroup){
			$arrWListTmp = $arrWList;
			$arrWListTmp["Odds"] = $flOddsAS;
			$arrWListTmp["MemExchangeRate"] = 1;
			$arrWListTmp["MemCurrency"] = 1;
			$arrWListTmp["MemStake"] = $arrAutoSuppleGold[$intBT][$intNum][$intUID];
			$arrWListTmp["MemPayOff"] = $arrAutoSuppleGold[$intBT][$intNum][$intUID]*($arrWListTmp["Odds"] - 1);
			$arrWListTmp["WagerType"] = $intGroup;

			$arrWContTmp = $arrWCont;
			$strCheckKey = str_replace("0.", $_data["CheckKey2"].$j.$intGroup.$intBT.$intNum."-", $_data["CheckKey1"]);
			$arrWContTmp["checkkey_x"] = $strCheckKey;
			$arrWContTmp["Content_x"] = $strWagerConditionsAS." (Auto)";

			$arrWListTmp["Layer6Commission"] = $arrCommission[$objMemberInfo -> Layer6ID][$intBT];
			$arrWListTmp["DirectorCommission"] = $arrCommission[$objMemberInfo -> DirectorID][$intBT];
			$arrWListTmp["StockCommission"] = $arrCommission[$objMemberInfo -> StockID][$intBT];
			$arrWListTmp["MainCommission"] = $arrCommission[$objMemberInfo -> MainID][$intBT];
			$arrWListTmp["SubCommission"] = $arrCommission[$objMemberInfo -> SubID][$intBT];

			$arrLayerScale = $objMemberInfo -> LayerScale[$intGroup];
			$arrWListTmp["SubRate"] = $arrLayerScale[2];
			$arrWListTmp["MainRate"] = $arrLayerScale[3];
			$arrWListTmp["StockRate"] = $arrLayerScale[4];
			$arrWListTmp["DirectorRate"] = $arrLayerScale[5];
			$arrWListTmp["Layer6Rate"] = $arrLayerScale[6];
			switch($intGroup){
				case 6:
					$arrWListTmp["DirectorID"] = $intUID;
					$arrWListTmp["DirectorCommission"] = $arrCommission[$intUID][$intBT];
				case 5:
					$arrWListTmp["StockID"] = $intUID;
					$arrWListTmp["StockCommission"] = $arrCommission[$intUID][$intBT];
				case 4:
					$arrWListTmp["MainID"] = $intUID;
					$arrWListTmp["MainCommission"] = $arrCommission[$intUID][$intBT];
				case 3:
					$arrWListTmp["SubID"] = $intUID;
					$arrWListTmp["SubCommission"] = $arrCommission[$intUID][$intBT];
				case 2:
					$arrWListTmp["MemID"] = $intUID;
					$arrWListTmp["MemCommission"] = $arrCommission[$intUID][$intBT];
					break;
			}
			$arrWContTmp["MemAccount_x"] = $objMemberInfo -> LayerAcc[$intGroup];

			switch($intGroup){
				case 2:
					$arrRealToday[$objMemberInfo -> MainID]["W"] += 1;
					$arrRealToday[$objMemberInfo -> MainID]["S"] += $arrWListTmp["MemStake"];
					$arrRealToday[$objMemberInfo -> MainID]["P"] += $arrWListTmp["MemPayOff"];
					$arrRealToday[$objMemberInfo -> MainID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["SubCommission"]*($arrLayerScale[2] - $arrLayerScale[3]);
					$arrRealToday[$objMemberInfo -> MainID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[2] - $arrLayerScale[3]);
					$arrRealToday[$objMemberInfo -> MainID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[2] - $arrLayerScale[3]);
				case 3:
					$arrRealToday[$objMemberInfo -> StockID]["W"] += 1;
					$arrRealToday[$objMemberInfo -> StockID]["S"] += $arrWListTmp["MemStake"];
					$arrRealToday[$objMemberInfo -> StockID]["P"] += $arrWListTmp["MemPayOff"];
					$arrRealToday[$objMemberInfo -> StockID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["MainCommission"]*($arrLayerScale[3] - $arrLayerScale[4]);
					$arrRealToday[$objMemberInfo -> StockID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[3] - $arrLayerScale[4]);
					$arrRealToday[$objMemberInfo -> StockID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[3] - $arrLayerScale[4]);
				case 4:
					$arrRealToday[$objMemberInfo -> DirectorID]["W"] += 1;
					$arrRealToday[$objMemberInfo -> DirectorID]["S"] += $arrWListTmp["MemStake"];
					$arrRealToday[$objMemberInfo -> DirectorID]["P"] += $arrWListTmp["MemPayOff"];
					$arrRealToday[$objMemberInfo -> DirectorID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[4] - $arrLayerScale[5]);
					$arrRealToday[$objMemberInfo -> DirectorID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[4] - $arrLayerScale[5]);
					$arrRealToday[$objMemberInfo -> DirectorID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[4] - $arrLayerScale[5]);
				case 5:
					$arrRealToday[$objMemberInfo -> Layer6ID]["W"] += 1;
					$arrRealToday[$objMemberInfo -> Layer6ID]["S"] += $arrWListTmp["MemStake"];
					$arrRealToday[$objMemberInfo -> Layer6ID]["P"] += $arrWListTmp["MemPayOff"];
					$arrRealToday[$objMemberInfo -> Layer6ID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[5] - $arrLayerScale[6]);
					$arrRealToday[$objMemberInfo -> Layer6ID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[5] - $arrLayerScale[6]);
					$arrRealToday[$objMemberInfo -> Layer6ID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[5] - $arrLayerScale[6]);
				case 6:
					break;
			}
			$arrRealToday[$intUID]["SW"] = 1;
			$arrRealToday[$intUID]["SS"] = $arrWListTmp["MemStake"];
			$arrRealToday[$intUID]["SP"] = $arrWListTmp["MemPayOff"];
			$arrRealToday[$intUID]["SC"] = $arrWListTmp["MemCommission"]*$arrWListTmp["MemStake"];

			$arrPlusStake["Stake"] +=  $arrWListTmp["MemStake"];
			$arrPlusStake["RealStake"] +=  $arrWListTmp["MemStake"]*$arrWListTmp["DirectorRate"];
			$arrPlusStake["PayOff"] +=  $arrWListTmp["MemPayOff"];
			$arrPlusStake["RealPayOff"] +=  $arrWListTmp["MemPayOff"]*$arrWListTmp["DirectorRate"];

			//寫入自動補貨注單
			$arrAutoSuppleBetData = InsertWagerToDB($arrWContTmp, $arrWListTmp);
			if($arrAutoSuppleBetData["isError"]){
				PMA_mysql_query("ROLLBACK", $_ConnMaster);
				$Err_Message = $arrAutoSuppleBetData["ErrorMessage"]."1";
				$Err_HiddenMessage = $arrAutoSuppleBetData["ErrorHiddenMessage"];
				include("wager_error.php");
			}
		}
	}

	//押碼跳動
	$arrJumpData = WagerJump($arrGDate, $arrWList["MemStake"], $arrWList["MemPayOff"], $_arrBetParam[$arrGDate["Bet_Type"]][$intAN], $arrGTAllOdds[$arrGDate["Bet_Type"]][$intAN], $arrWList["MemExchangeRate"]*$arrWList["DirectorRate"], $arrWList["DirectorCommission"], $arrPlusStake);
	if($arrJumpData["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
		$Err_Message .= "<br />".$strBTTip;
		$Err_HiddenMessage = $GLOBALS["errorhidden"];
		include("wager_error.php");
	}

	$arrTmp = array();
	$arrTmp[$intBT][$intNum] = $arrRealToday;
	if(!setCountWagerForAllLayers($intLID, $intGID, $arrTmp)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
		$Err_HiddenMessage = $GLOBALS["errorhidden"]."6";
		include("wager_error.php");
	}

	$arrSucc[$j]["BetID"] = $arrBetData["BetID"];
	$arrSucc[$j]["StakeMoneyValue"] = $intGold;
	$arrSucc[$j]["WagerMessage"] = WagerFormTrans($arrGDate["LottoID"], $arrGDate["Bet_Type"], $strWagerConditions);
	$arrSucc[$j]["PayOffValue"] = $flMemPayOff;
	$arrLogData[$j] = $arrBetData["LogData"];
	$j++;
}  // while(list($k, $strWData) = each($arrWC))
//產生累積注額陣列
if(!getWagerArrayGold($objMemberInfo -> ID)){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}
//交易完成
PMA_mysql_query("COMMIT", $_ConnMaster);
AddData_Log($objMemberInfo -> ID,"下注成功 ", "".join("\n", $arrLogData));
$Err_Message = "Success!!";
@mysql_close($$_Conn);
include("wager_success.php");
?>